#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

static int is_running = 1;

void use_resource()
{
    printf("use resource\n");
}
void free_resource()
{
    printf("free resource\n");
}
void exit_handler(int sig)
{
    if (sig == SIGINT)
    {
        printf("收到SIGINT\n");
        is_running = 0;
    }
    else
    {
        printf("收到SIGTERM\n");
        free_resource();
        exit(0);
    }
    
}
int main(int argc, char const *argv[])
{
    //注册信号处理函数
    signal(SIGINT, exit_handler);  //crtl + c
    signal(SIGTERM, exit_handler); //kill 进程
    use_resource();
    while (is_running)
    {
        printf("running...\n");
        sleep(1);
    }
    
    free_resource();
    return 0;
}
